package java8.stream;

import utils.Student;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Description: Stream 流式操作练习
 *
 * @author Cheng Jun
 * @version 1.0
 * @date 2019/10/30 10:55
 */
public class StreamExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        // list.stream().map(element -> Integer.valueOf(element)).collect(Collectors.toList());
        List<Integer> integerList = list.stream().mapToInt(element -> Integer.valueOf(element)).boxed().collect(Collectors.toList());
        integerList.forEach(element -> System.out.println(element));

        Map<String, BigDecimal> bigDecimalMap = new LinkedHashMap<>(8);
        bigDecimalMap.put("1", BigDecimal.valueOf(1));
        bigDecimalMap.put("2", BigDecimal.valueOf(4));
        // reduce 求和函数
        BigDecimal reduce = bigDecimalMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println(reduce);

        bigDecimalMap.values().forEach(data -> data = data.add(BigDecimal.valueOf(1)));

        bigDecimalMap.forEach((key, value) -> bigDecimalMap.replace(key, value.add(BigDecimal.TEN)));
        bigDecimalMap.forEach((key, value) -> System.out.println(value));

        List<String> deptNames = new ArrayList<>();
        deptNames.add("Balkhash CRB");

        deptNames.add("Balkhash Centr Vakcinacy");
        deptNames.add("Enbekshi");
        deptNames.add("Eskeldi CRB");
        deptNames.add("Kapshagay GB");
        deptNames.add("Karatal CRB");
        deptNames.add("Kegen RB");
        deptNames.add("Panfilov mrb");
        deptNames.add("Tekeli GB");
        deptNames.add("Zhambyl CRB");
        deptNames.add("taldik.pol");
        deptNames.add("taldik.pol2");

        // List<String> sortList = deptNames.stream().sorted(Comparator.comparing(deptName -> deptName.toUpperCase())).collect(Collectors.toList());
        List<String> sortList = deptNames.stream().sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList());
        System.out.println(sortList);

        Map<String, Integer> reduceMap = new HashMap<>();
        // reduceMap.put("salary",1000);
        // reduceMap.put("fund",1000);
        Integer money = reduceMap.values().stream().reduce(0, Integer::sum);
        System.out.println(money);
        System.out.println("2021-01-01".substring(0, 10));
        System.out.println("J".split(",")[0]);

        Student student1 = new Student();
        student1.setName("xm");
        Student student2 = new Student();
        student2.setName("laowu");
        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        String nameString = students.stream().map(Student::getName).collect(Collectors.joining(","));
        System.out.println(nameString);

        List<String> teamIndia = Arrays.asList("Virat", "Dhoni", "DDD");
        List<String> teamAustralia = Arrays.asList("Warner", "Watson", "Smith");
        List<String> teamEngland = Arrays.asList("Alex", "Bell", "Broad");

        List<String> nameList = Stream.of(teamAustralia, teamEngland, teamIndia).flatMap(team -> team.stream()).collect(Collectors.toList());
        for (String name : nameList) {
            System.out.println(name);
        }

        // Map<String, String> imeiDevNameMap = iotDeviceInfoList.stream()
        //         .collect(Collectors.toMap(DeviceInfoDocument::getDeviceImei, DeviceInfoDocument::getDevName, (deviceImei, devName) -> devName));
        // List<Student> students = new ArrayList<>();
        // Student student1 = new Student();
        // student1.setName("cj");
        // Student student2 = new Student();
        // student2.setName("xx");
        // students.add(student1);
        // students.add(student2);
        // Student student = students.stream().max(Comparator.comparing(Student::getName, String::compareTo)).get();
        // System.out.println(student);

        List<Student> testDistinct = new ArrayList<>();
        Student student10 = new Student();
        student10.setName("cj");
        student10.setAge(29);

        Student student20 = new Student();
        student20.setName("cj");
        student20.setAge(30);

        testDistinct.add(student10);
        testDistinct.add(student20);
        // testDistinct.stream().distinct()



    }
}
